package com.swingson.properties.impl;

import com.swingson.SwingsonGui;
import com.swingson.exceptions.SwingsonException;
import com.swingson.jsonparser.JsonElement;
import com.swingson.properties.impl.PropertyProcessors.PrimitivePropertyProcessor;
import javax.swing.JComponent;

/**
 * @author sad
 */
public class IdPropertyProcessor extends PrimitivePropertyProcessor {

    @Override
    protected Object getValue(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
        String value = jsonElement.getAsString();
        if (gui.getDefinedComponent(value) != null) {
            throw new SwingsonException(String.format("Component with Id \"%s\" already exists. Assign another id", value));
        }
        gui.addDefinedComponent(value, null, component);
        component.putClientProperty("id", value);
        return component;
    }
}
